/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * @author Renato ferreira dos santos
 * Created on 30/03/2010, 17:38:21
 */

package rad.Java.MotelPlus.Views;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rad.Java.Common.DB.Database;

public class frmViewMovement extends javax.swing.JDialog {

    // <editor-fold defaultstate="collapsed" desc="Declaration of variables">
        //Format formatter;
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        DefaultTableModel dtmMoviment = new DefaultTableModel();
    // </editor-fold>

    /** Creates new form frmViewMovement */
    public frmViewMovement() {
        setModal(true);
        initComponents();
        fillDay();
   }

    // <editor-fold defaultstate="collapsed" desc="functions">
       public void fillDay(){
            //Return First Day Month
            tty1.setValue(formatter.format(rad.Java.Common.Util.Functions.primeiroDiaMesD(0, 0)));//First Day Month

            //Return Last Day Month
            tty2.setValue(formatter.format(rad.Java.Common.Util.Functions.ultimoDiaMesD(0, 0)));//Last Day Month
           
       }

        public void fillTblMain(){
            try {
                clearTblMain();
                String[] produtos = new String[11];
                Double amount = 0.0;
                long cont = 0;
                long increment = 0;
                long discount = 0;

                String sql = "Select * from v_moviment  WHERE data_ini BETWEEN  '"+
                             rad.Java.Common.Util.Functions.formatDateInternational(tty1.getText(),"dd/MM/yyyy")+
                             "' AND '"+
                             rad.Java.Common.Util.Functions.formatDateInternational(tty2.getText(),"dd/MM/yyyy")
                             +"'";
                ResultSet rs = Database.executeQueryRs(sql);
                while (rs.next()) {
                    produtos[0] = rs.getString("stay_id");
                    produtos[1] = rs.getString("data");
                    produtos[2] = rs.getString("hora_entrada");
                    produtos[3] = rs.getString("hora_saida");
                    produtos[4] = rs.getString("tempo");
                    produtos[5] = "";//Funcionario
                    produtos[6] = rs.getString("apt_number");
                    produtos[7] = rs.getString("apt_type_short_name");
                    produtos[8] = rs.getString("stay_discount");
                    produtos[9] = rs.getString("stay_increment");
                    produtos[10] = rs.getString("stay_total");
                    discount  += Double.valueOf(produtos[8]);
                    increment += Double.valueOf(produtos[9]);
                    amount    += Double.valueOf(produtos[10]);
                    dtmMoviment.addRow(produtos);
                    cont++;
                }
                dtmMoviment.removeRow(0);
                tblMain.setModel(dtmMoviment);
                txtQuantMove.setText(String.valueOf(cont));
                txtTotal.setText(String.valueOf(amount));
                txtTotalDesconto.setText(String.valueOf(discount));
                txtTotalAcrescimo.setText(String.valueOf(increment));
                txtTotalGeral.setText(String.valueOf(amount + increment - discount));
                //txtConsumo.setText(String.valueOf(sumAll));
            } catch (SQLException ex) {
                Logger.getLogger(frmViewMovement.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        private void clearTblMain()
        {
            String[] colunas;
            colunas = new String[]{"Cod","Data","Hora Entrada","Hora Saida","Tempo","Operador","Nº Quarto","Tipo Quarto","Desconto","Acrescimo","Total"};
            String[][] dados = new String[][]{{}};
            dtmMoviment = new DefaultTableModel(dados, colunas);
            tblMain.setModel(dtmMoviment);
        }
    // </editor-fold>

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tty2 = new JFormattedTextField(DateFormat.getDateInstance(DateFormat.SHORT));
        tty1 = new javax.swing.JFormattedTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblMain = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtTotalDesconto = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtQuantMove = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtTotalAcrescimo = new javax.swing.JTextField();
        txtTotalGeral = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(motelplus.MotelPlusApp.class).getContext().getResourceMap(frmViewMovement.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        try {
            tty2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tty2.setName("tty2"); // NOI18N

        try {
            tty1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tty1.setName("tty1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(tty1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tty2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 320, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jLabel2)))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tty1, tty2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tty2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(tty1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblMain.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblMain.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tblMain.setName("tblMain"); // NOI18N
        jScrollPane2.setViewportView(tblMain);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtTotal.setText(resourceMap.getString("txtTotal.text")); // NOI18N
        txtTotal.setName("txtTotal"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtTotalDesconto.setText(resourceMap.getString("txtTotalDesconto.text")); // NOI18N
        txtTotalDesconto.setName("txtTotalDesconto"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtQuantMove.setText(resourceMap.getString("txtQuantMove.text")); // NOI18N
        txtQuantMove.setName("txtQuantMove"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtTotalAcrescimo.setText(resourceMap.getString("txtTotalAcrescimo.text")); // NOI18N
        txtTotalAcrescimo.setName("txtTotalAcrescimo"); // NOI18N

        txtTotalGeral.setText(resourceMap.getString("txtTotalGeral.text")); // NOI18N
        txtTotalGeral.setName("txtTotalGeral"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtQuantMove)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTotalDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTotalAcrescimo, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(txtTotalGeral, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE)))
                .addGap(33, 33, 33))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtTotal, txtTotalGeral});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtQuantMove, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTotalDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTotalAcrescimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTotalGeral, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-662)/2, (screenSize.height-403)/2, 662, 403);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(!rad.Java.Common.Util.Functions.DateValidation(tty1.getText(), "dd/MM/yyyy"))
        {
            JOptionPane.showMessageDialog(null, "Data incorreta");
            return;
        }

        if(!rad.Java.Common.Util.Functions.DateValidation(tty2.getText(), "dd/MM/yyyy"))
        {
            JOptionPane.showMessageDialog(null, "Data incorreta");
            return;
        }

        fillTblMain();
    }//GEN-LAST:event_jButton1ActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblMain;
    private javax.swing.JFormattedTextField tty1;
    private javax.swing.JFormattedTextField tty2;
    private javax.swing.JTextField txtQuantMove;
    private javax.swing.JTextField txtTotal;
    private javax.swing.JTextField txtTotalAcrescimo;
    private javax.swing.JTextField txtTotalDesconto;
    private javax.swing.JTextField txtTotalGeral;
    // End of variables declaration//GEN-END:variables

}
